<?php
Yii::import('application.components.dao.class.AuctionCreateOrder');
Yii::import('application.components.auctionCache.AuctionCacheData');
Yii::import('application.components.auctionCache.AuctionHistoryList');
Yii::import('application.components.auctionCache.customerCacheAuctionBidInfo');
class AuctionClear{
	public $auctionId;
	
	public function clear(){
		$processItems=$this->processItems();
		if (!empty($processItems)) {
			foreach ($processItems as $processKey=>$item){
				if (method_exists($this, $item)) {
					$this->$item();
				}else{
					throw new CException(Yii::t('yii', 'Method is not found!'));
				}
			}	
		}		
	}
	
	public function processItems(){
		return array(
		    'createOrder'		=>'createOrder',
			'auctionCacheData'	=>'auctionCacheData',
			'auctionHistory'	=>'auctionCacheHistory',
			'auctionCustomer'	=>'auctionCacheCustomer',
			'autoBidClear'		=>'autoBidClear'
		);
	}
	
	public function auctionLimitClear(){
		$limit=new AuctionLimit();
		return $limit->deleteAllByAttributes(array('auction_id'=>$this->auctionId,'limit_type'=>'AUTO_BID'));
	}
	
	public function autoBidClear(){
		$autobid=new Autobid();
		$autobidList=$autobid->findAllByAttributes(array('auction_id'=>$this->auctionId));
		foreach ($autobidList as $autobidder){
		    $autobidLog=new AutobidLog();
		    $autobidLog->attributes=$autobidder->attributes;
		    $autobidLog->save();
		}
		return Autobid::model()->deleteAllByAttributes(array('auction_id'=>$this->auctionId));
	}
	
	public function auctionCacheCustomer(){
		$auctionCacheCustomer=new customerCacheAuctionBidInfo();
		return $auctionCacheCustomer->delItems($this->auctionId);	
	}
	
	public function auctionCacheHistory(){
		$auctionHistory=new AuctionHistoryList();
		return $auctionHistory->delItems($this->auctionId);
	}
	
	public function auctionCacheData(){
		$auctionCacheData=new AuctionCacheData();
		$auctionCacheData->setExpire($this->auctionId);
	}
	
	public function createOrder(){
		$auctionBidLog=new AuctionBidLog();
		$auctionBidLog=$auctionBidLog->findByAttributes(array('auction_id'=>$this->auctionId));
		if(!empty($auctionBidLog)){
			$order=new AuctionCreateOrder();
			$order->auctionId=$this->auctionId;
			$order->createOrder();	
		}
	}
	
	
	public function clearAuctionLimit(){
		$auctionLimit=new AuctionLimit();
		return $auctionLimit->deleteAllByAttributes(array('auction_id'=>$this->auctionId,'limit_type'=>'AUTO_BID'));
	}
	
}